package com.mrdeer.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.mrdeer.common.domain.BusinessException;
import com.mrdeer.common.domain.CommonErrorCode;
import com.mrdeer.merchant.api.AppService;
import com.mrdeer.merchant.api.dto.AppDTO;
import com.mrdeer.merchant.api.entity.App;
import com.mrdeer.merchant.api.entity.Merchant;
import com.mrdeer.merchant.convert.AppConvert;
import com.mrdeer.merchant.mapper.AppMapper;
import com.mrdeer.merchant.mapper.MerchantMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

/**
 * @author ：xie jianchu
 * @date ：Created in 2020/9/10 0010 10:41
 */
@Service
public class AppServiceImpl implements AppService {

    @Autowired
    private AppMapper appMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    /**
     * 1.校验商户是否通过资质审核，如果商户资质审核没有通过不允许创建应用
     * 2.生成应用ID，应用ID使用UUID方式生成
     * 3.保存商户应用信息，应用名称需要校验唯一性
     *
     * @param merchantId 商户ID
     * @param appDTO     应用信息
     * @return
     * @throws BusinessException
     */
    @Override
    public AppDTO createApp(Long merchantId, AppDTO appDTO) throws BusinessException {
        if (merchantId == null || appDTO == null || appDTO.getAppName() == null) {
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        //1.校验商户资质是否审核
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (merchant == null) {
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        //取出商户资质
        String auditStatus = merchant.getAuditStatus();
        if (!"2".equals(auditStatus)) {
            throw new BusinessException(CommonErrorCode.E_200003);
        }
        if (isExistAppName(appDTO.getAppName())) {
            throw new BusinessException(CommonErrorCode.E_200004);
        }
        //2.生成应用ID
        String appId = UUID.randomUUID().toString();
        //TODO:应用名称需要校验唯一性
        App entity = AppConvert.INSTANCE.dto2Entity(appDTO);
        entity.setAppId(appId);
        entity.setMerchantId(merchantId);
        //调用appMapper向app表插入数据
        appMapper.insert(entity);
        return AppConvert.INSTANCE.entity2Dto(entity);
    }

    /**
     * 判断应用是否存在
     *
     * @param appName 应用名称
     * @return
     */
    private Boolean isExistAppName(String appName) {
        int count = appMapper.selectCount(new LambdaQueryWrapper<App>().eq(App::getAppName, appName));
        return count > 0;
    }

    /**
     * 根据商户ID查询应用列表
     *
     * @param merchantId 商户ID
     * @return
     * @throws BusinessException
     */
    @Override
    public List<AppDTO> queryAppByMerchantId(Long merchantId) throws BusinessException {
        List<App> apps = appMapper.selectList(new LambdaQueryWrapper<App>().eq(App::getMerchantId, merchantId));
        return AppConvert.INSTANCE.listEntity2Dto(apps);
    }

    /**
     * 根据应用id查询应用信息
     *
     * @param appId APP ID
     * @return
     * @throws BusinessException
     */
    @Override
    public AppDTO getAppByAppId(String appId) throws BusinessException {
        App app = appMapper.selectOne(new LambdaQueryWrapper<App>().eq(App::getAppId, appId));
        return AppConvert.INSTANCE.entity2Dto(app);
    }

    /**
     * 校验应用是否属于商户
     *
     * @param appId      应用ID
     * @param merchantId 商户ID
     * @return true 存在 false 不存在
     */
    @Override
    public Boolean queryAppInMerchant(String appId, Long merchantId) {
        Integer count = appMapper.selectCount(new LambdaQueryWrapper<App>().eq(App::getAppId, appId).eq(App::getMerchantId, merchantId));
        return count > 0;
    }
}
